<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class LinkController extends Controller
{
    /**
    *  选择申请方式页面
    */
    public function getIndex(Request $request)
    {   
        // 获取店铺审核表(link)里面的数据信息
        $data = DB::table('link') -> orderBy('state','asc') -> orderBy('time','asc')  -> paginate(); 

        return view('home.link.index',['data'=>$data,'request'=>$request -> all()]);
    } 

    /**
    *  在link表中刚插入状态的最后一条数据
    */
    public function postInsert(Request $request)
    {
        if($request){
            $mode = $request -> input('mode');
            // 插入数据
            // 自动验证表单
            if ($mode == 1) {
                $this -> validate($request,[
                'phone' => 'required',
                'email' => 'required|email',
                'timelength' => 'required',
                'words' => 'required',
                'url' => 'required|url',
                'contents' => 'required',
                ],
                [
                    'phone.required' => '手机号码不能为空',
                    'email.required' => '邮箱不能为空',
                    'email.email' => '邮箱格式不正确',
                    'timelength.required' => '请填写要开通的天数',
                    'words.required' => '连接名不能为空', 
                    'url.required' => 'url地址不能为空',
                    'url.url' => 'url地址格式不正确',
                    'contents.required' => '链接描述不能为空不能为空',
                ]); 
            }else if($mode == 2){
                $this -> validate($request,[
                'phone' => 'required',
                'email' => 'required|email',
                'timelength' => 'required',
                'image' => 'required', 
                'url' => 'required|url',
                'contents' => 'required',
                ],
                [
                    'phone.required' => '手机号码不能为空',
                    'email.required' => '邮箱不能为空',
                    'email.email' => '邮箱格式不正确',
                    'timelength.required' => '请填写要开通的天数',
                    'image.required' => '图片不能为空',
                    'url.required' => 'url地址不能为空',
                    'url.url' => 'url地址格式不正确',
                    'contents.required' => '链接描述不能为空不能为空',
                ]);

            }
          
            // 数据处理
            $arr = $request -> except('_token');
            // dd($arr);
            if ($mode == 2) {
                if ($request->hasFile('image')) {
                    // 获取图片后缀
                    $suffix = $request -> file('image') -> getClientOriginalExtension();

                    //设置图片名称 
                    $name = date('YmdHis',time());

                    // 判断图片后缀名
                    $array = ['jpg','png','gif','jpeg'];
                    if (in_array($suffix,$array)) {
                        $arr['image'] = '/ad/linkimage/'.$name.'.'.$suffix;
                        $request -> file('image') -> move('./ad/linkimage',$name.'.'.$suffix);
                    }else{
                       return back() -> with('error','文件格式不正确');
                    }
                }
            }
           
            // 获取提交时间，并压入$arr中
            $arr['time'] = time();

            // 修改数据库
            $res = DB::table('link') -> insert($arr);

            // 判断返回的影响行数 把操作的结果压入session
            if ($res) {
                return redirect('/home/link/index') -> with('success','申请已提交，请等待管理员审核！');
            }else{
                return back() -> with('error','申请失败');
            }
        }
    }
 
}
